package com.t.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.logging.Logger;

/**
 * Created by Administrator on 2018/5/16.
 */
public class testInterceptor implements HandlerInterceptor {

    private static final String error_msg = "用户未登录!";

    //private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final String[] noFilters = new String[]{"/login","/home/login/verifycode/get","/home/onlogin","/loginout","/Run/*"};


    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        //logger.info("sessionId:{}", request.getSession().getId());
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.addHeader("Access-Control-Allow-Headers", "Content-Type,Accept");

        String url = request.getRequestURI();
        for (String no : noFilters) {
            if (url.endsWith(String.format("%s%s", request.getContextPath(), no)))
                return true;
        }
        //logger.info(request.getSession().getId());
       // CurrentManager currentManager = (CurrentManager)request.getSession().getAttribute(ApplicationSessionKeys.CURRENT_MANAGER);
        if(false){
            return true;
        } else {

            return true;
            //response.sendRedirect(request.getContextPath() + "/login");
            //response.setContentType("text/html");
            //PrintWriter out = response.getWriter();
            //out.println("<html>");
            //out.println("<script>");
            //out.println("window.open ('"+request.getContextPath()+"/login','_top')");
            //out.println("</script>");
            //out.println("</html>");

           // return false;
        }

    }



    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        response.setContentType("application/json;charset=UTF-8");
    }


    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        response.setContentType("application/json;charset=UTF-8");
    }


    private Object getPort(HttpServletRequest request) {
        return request.getLocalPort();
    }
}
